- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathinbox.py
38 lines (33 loc) · 1.19 KB
/
inbox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
importimaplib
importemail
host='imap.gmail.com'
username='hungrypy@gmail.com'
password='<your password>'
defget_inbox():
mail=imaplib.IMAP4_SSL(host)
mail.login(username, password)
mail.select("inbox")
_, search_data=mail.search(None, 'UNSEEN')
my_message= []
fornuminsearch_data[0].split():
email_data= {}
_, data=mail.fetch(num, '(RFC822)')
# print(data[0])
_, b=data[0]
email_message=email.message_from_bytes(b)
forheaderin ['subject', 'to', 'from', 'date']:
print("{}: {}".format(header, email_message[header]))
email_data[header] =email_message[header]
forpartinemail_message.walk():
ifpart.get_content_type() =="text/plain":
body=part.get_payload(decode=True)
email_data['body'] =body.decode()
elifpart.get_content_type() =="text/html":
html_body=part.get_payload(decode=True)
email_data['html_body'] =html_body.decode()
my_message.append(email_data)
returnmy_message
if__name__=="__main__":
my_inbox=get_inbox()
print(my_inbox)
# print(search_data)